package com.bootdo.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.system.dao.CourseMapper;
import com.bootdo.system.dao.CourseTimeMapper;
import com.bootdo.system.dao.CustomerMapper;
import com.bootdo.system.dao.OrderMapper;
import com.bootdo.system.domain.CourseTime;
import com.bootdo.system.domain.Order;
import com.bootdo.system.service.OrderService;
import com.github.pagehelper.PageHelper;



@Service("orderService")
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private CourseTimeMapper courseTimeMapper;
	
	@Autowired
	private CourseMapper courseMapper;
	
	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public Order get(Long id){
		Order order =  orderMapper.get(id);
		CourseTime courseTime = courseTimeMapper.get(order.getCourseId());
		if(courseTime != null) {
			order.setCourseTime(courseTime);
			order.setCourse(courseMapper.get(courseTime.getCourseId()));
		}
		order.setCustomer(customerMapper.get(order.getCustomerId()));
		return order;
	}
	
	@Override
	public List<Order> list(Map<String, Object> map){
		PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
		return orderMapper.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return orderMapper.count(map);
	}
	
	@Override
	public int save(Order bComments){
		return orderMapper.save(bComments);
	}
	
	@Override
	public int update(Order bComments){
		return orderMapper.update(bComments);
	}
	
	@Override
	public int remove(Long id){
		return orderMapper.remove(id);
	}
	
	@Override
	public int batchRemove( Integer[] ids){
		return orderMapper.batchRemove(ids);
	}

	@Override
	public Order getByNo(String orderNo) {
		Order order =  orderMapper.getByNo(orderNo);
		order.setCourse(courseMapper.get(order.getCourseId()));
		return order;
	}

	
	@Override
	public List<Order> getOutTimeOrders(){
		return orderMapper.getOutTimeOrders();
	}

	@Override
	public int cancelOrder(Long id) {
		Order order = get(id);
		if(order != null) {
			CourseTime courseTime = courseTimeMapper.get(order.getCourseId());
			if(courseTime != null) {
				courseTime.setRemainderNumber(courseTime.getRemainderNumber()+1);
				courseTime.setSignedNumber(courseTime.getSignedNumber() -1);
				if(!"".equals(courseTime.getCourseStatus())) {
					courseTime.setCourseStatus("1");
				}
				courseTimeMapper.update(courseTime);
			}
			order.setOrderStatus(2L);
			if (update(order) > 0) {
				return 1;
			} else {
				return 0;
			}
		}else {
			return -1;
		}
	}
	
	
	@Override
	public int updateOrderStatus(Long id,Long orderStatus) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("id", id);
		param.put("orderStatus", orderStatus);
		return orderMapper.updateOrderStatus(param);
	}

	@Override
	public List<Order> listByTime(Map<String, Object> map) {
		PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
		return orderMapper.listByTime(map);
	}
	
}
